package com.ruoyi.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.inventory.domain.vo.ProductBomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.inventory.domain.ProductStructDetail;
import com.ruoyi.inventory.mapper.ProductStructMapper;
import com.ruoyi.inventory.domain.ProductStruct;
import com.ruoyi.inventory.service.IProductStructService;

/**
 * 产品结构清单Service业务层处理
 * 
 * @author xwj
 * @date 2024-08-09
 */
@Service
public class ProductStructServiceImpl extends ServiceImpl<ProductStructMapper, ProductStruct> implements IProductStructService {
    @Autowired
    private ProductStructMapper productStructMapper;

    /**
     * 查询产品结构清单
     * 
     * @param id 产品结构清单主键
     * @return 产品结构清单
     */
    @Override
    public ProductStruct selectProductStructById(Long id) {
        return productStructMapper.selectProductStructById(id);
    }

    /**
     * 查询产品结构清单列表
     * 
     * @param productStruct 产品结构清单
     * @return 产品结构清单
     */
    @Override
    public List<ProductStruct> selectProductStructList(ProductStruct productStruct) {
        return productStructMapper.selectProductStructList(productStruct);
    }

    /**
     * 新增产品结构清单
     * 
     * @param productStruct 产品结构清单
     * @return 结果
     */
    @Transactional
    @Override
    public ProductStruct insertProductStruct(ProductStruct productStruct) {

        save(productStruct);
        insertProductStructDetail(productStruct);
        return productStruct;
    }

    /**
     * 修改产品结构清单
     * 
     * @param productStruct 产品结构清单
     * @return 结果
     */
    @Transactional
    @Override
    public ProductStruct updateProductStruct(ProductStruct productStruct) {

        productStructMapper.deleteProductStructDetailByStructId(productStruct.getId());
        insertProductStructDetail(productStruct);
      //  productStructMapper.updateProductStruct(productStruct);
        updateById(productStruct);
        return productStruct;
    }

    /**
     * 批量删除产品结构清单
     * 
     * @param ids 需要删除的产品结构清单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductStructByIds(Long[] ids) {
        productStructMapper.deleteProductStructDetailByStructIds(ids);
        return productStructMapper.deleteProductStructByIds(ids);
    }

    /**
    * 条件删除
    * @param productStruct 产品结构清单
    * @return
    */
    @Override
    public int delete(ProductStruct productStruct) {
        QueryWrapper<ProductStruct> qw = new QueryWrapper<>();
        QueryTool.condition(qw, productStruct);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     * @param productStruct 产品结构清单
     * @return
     */
    @Override
    public Pages<ProductStruct> queryPage(ProductStruct productStruct) {
        QueryWrapper<ProductStruct> qw = new QueryWrapper<>();
        QueryTool.condition(qw, productStruct);
        IPage<ProductStruct> ipage = new Query<ProductStruct>().getPage(productStruct);

        IPage<ProductStruct> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    @Override
    public List<ProductBomVo> selectProductBom(Long productId) {
        List<Long> bomProductIds = productStructMapper.selectBomProductIds(); // 所有配置了BOM的产品id
        int level = 1;
        List<ProductBomVo> list = this.loopBom(bomProductIds, productId, level);
        return list;
    }

    private List<ProductBomVo> loopBom(List<Long> bomProductIds,  Long productId, int level) {
        List<ProductBomVo> list = productStructMapper.selectProductBom(productId);
        if(list != null && list.size() > 0) {
            for(ProductBomVo vo : list) {
                vo.setLevel(level);
                if(bomProductIds.contains(vo.getProductId())) {
                    List<ProductBomVo> children = loopBom(bomProductIds, vo.getProductId(),level+1);
                    vo.setChildren(children);
                }
            }
        }
        return list;
    }

    /**
     * 删除产品结构清单信息
     * 
     * @param id 产品结构清单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductStructById(Long id) {
        productStructMapper.deleteProductStructDetailByStructId(id);
        return productStructMapper.deleteProductStructById(id);
    }

    /**
     * 新增产品结构清单-详情信息
     * 
     * @param productStruct 产品结构清单对象
     */
    public void insertProductStructDetail(ProductStruct productStruct) {
        List<ProductStructDetail> productStructDetailList = productStruct.getProductStructDetailList();
        Long id = productStruct.getId();
        if (StringUtils.isNotNull(productStructDetailList))
        {
            List<ProductStructDetail> list = new ArrayList<ProductStructDetail>();
            for (ProductStructDetail productStructDetail : productStructDetailList)
            {
                productStructDetail.setStructId(id);
                list.add(productStructDetail);
            }
            if (list.size() > 0)
            {
                productStructMapper.batchProductStructDetail(list);
            }
        }
    }
}
